package cn.controller;

import cn.basic.BaseController;
import cn.dao.SystemLogDAO;
import cn.factory.DAOFactory;
import cn.util.Expression;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping(value="systemlog")
public class SystemLogController extends BaseController {


    @RequestMapping(value="getsystemloglist")
    public void getsystemloglist(
            Integer page,
            Integer limit,
            String logtype,
            String opermethod,
            String userid,
            String date,
            HttpServletRequest request,
            HttpServletResponse response,
            Model model) throws IOException{

        //根据传入的参数构建查询条件
        Expression exp = new Expression();
        if(logtype != null && !logtype.equals(""))
            exp.andEqu("opertype", logtype, String.class);
        if(opermethod != null && !opermethod.equals(""))
            exp.andEqu("opermethod", opermethod, String.class);
        if(userid != null && !userid.equals(""))
            exp.andEqu("userid", userid, String.class);
        if(date != null && !date.equals(""))
            exp.andEqu("createdate", date, String.class);
        String wherecondition = exp.toString();

        SystemLogDAO  sylogdao = DAOFactory.getSystemLogDAO();
        List list = sylogdao.getSystemLogList(wherecondition, page, limit);
        int size = sylogdao.getSystemLogAmount(wherecondition);

        if(list!=null){
            dt.code = dt.FLAG_SUCC;
            dt.count = size;
            dt.msg = "查询成功，共查出" + dt.count + "条记录";
            dt.data = list;
        }else{
            dt.code = dt.FLAG_FAIL;
            dt.msg = "查询失败";
        }
        closeout();
    }

    @RequestMapping(value="/deleteAllLog")
    public void deleteAllLog(
            HttpServletRequest request,
            HttpServletResponse response,
            Model model) throws IOException{
        SystemLogDAO  sylogdao = DAOFactory.getSystemLogDAO();
        if(sylogdao.deleteAllLog()){
            dt.code = dt.FLAG_FAIL;
            dt.msg = "删除成功";
        }else{
            dt.code = dt.FLAG_FAIL;
            dt.msg = "删除失败";
        }
        closeout();
    }
}
